#include "monitorTimer.h"

typedef struct{
    QueueHandle_t xEventQueue;
    int16_t tmpData;
    void* pData;
}MonitorPara;

static TimerHandle_t xTimer = NULL;
static MonitorPara *paraData = NULL;

static int16_t myId = 1;

/*-----------------------------------------------------------*/
static void vTimerCallback( TimerHandle_t pxTimer )
{
	long lTimerId;
	configASSERT( pxTimer );

	lTimerId = ( long ) pvTimerGetTimerID( pxTimer );

	if (lTimerId != TIMER_ID) {
		xil_printf("FreeRTOS Hello World Example FAILED");
	}

    Event event;
    int16_t *pData = (int16_t*)pvPortMalloc(sizeof(int16_t));
    *pData = myId;
    makeEvent(&event, myId, EVENT_MONITOR_TIMEOUT, pData);
    xQueueSend( paraData->xEventQueue, &event, 0UL );

}

int start_monitor(QueueHandle_t xEventQueue){

    paraData = (MonitorPara*)pvPortMalloc(sizeof(MonitorPara));

    paraData->xEventQueue = xEventQueue;
    paraData->tmpData = 0;

    const TickType_t x10seconds = pdMS_TO_TICKS( DELAY_10_SECONDS );
    const TickType_t x1seconds = pdMS_TO_TICKS( DELAY_1_SECOND );

	xTimer = xTimerCreate( (const char *) "Timer",
							x1seconds,
							pdTRUE,
							(void *) TIMER_ID,
							vTimerCallback);
	/* Check the timer was created. */
	configASSERT( xTimer );

	xTimerStart( xTimer, 0 );

    return 0;
}